package com.wanpinghui.supplier.message;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.githang.statusbar.StatusBarCompat;
import com.wanpinghui.supplier.R;
import com.wanpinghui.supplier.product.ProductListActivity;
import com.wanpinghui.supplier.user.order.HistoryOrderActivity;
import com.wanpinghui.supplier.widgets.TitlePanelLayoutUser;
import com.yunshl.yunshllibrary.view.YunShlPopupWinow;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

/**
 * 功能说明：
 * Created by caitiangui
 * 创建时间： 2017/12/25.
 */
@ContentView(R.layout.layout_activity_message_detail)
public class MessageDetailActivity extends AppCompatActivity {

    @ViewInject(R.id.tpl_title)
    private TitlePanelLayoutUser tpl_title;

    @ViewInject(R.id.fl_history_order)
    private FrameLayout fl_history_order;

    @ViewInject(R.id.fl_trade)
    private FrameLayout frameLayoutTrade;

    private YunShlPopupWinow popupWinow;
    private int userId;
    private int mineId;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
        tpl_title.setTitleBg(ContextCompat.getColor(this, R.color.colorPrimaryBlue));
        tpl_title.setCenterTitleTextColor(ContextCompat.getColor(this, R.color.white));
        tpl_title.setBackRes(ContextCompat.getDrawable(this, R.mipmap.common_icon_return_white));
        StatusBarCompat.setStatusBarColor(this, ContextCompat.getColor(this, R.color.colorPrimaryBlue), false);
        if (getIntent() != null) {
            userId = getIntent().getIntExtra("user_id", 0);
            mineId = getIntent().getIntExtra("mine_id", 0);
        }
        bindEvents();
    }

    private void bindEvents() {
        tpl_title.setOnClickBacktrack(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        fl_history_order.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MessageDetailActivity.this, HistoryOrderActivity.class);
                intent.putExtra("id", userId);
                startActivity(intent);
            }
        });

        frameLayoutTrade.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //showTradeView();
                Intent intent = new Intent(MessageDetailActivity.this, ProductListActivity.class);
                intent.putExtra("buyer", mineId);
                intent.putExtra("supplier", userId);
                startActivity(intent);
            }
        });
    }

    private void showTradeView() {
        View view = LayoutInflater.from(MessageDetailActivity.this).inflate(R.layout.view_trade, null);
        if (popupWinow == null)
            popupWinow = new YunShlPopupWinow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWinow.setCommonConfig(MessageDetailActivity.this, YunShlPopupWinow.AnimaDirect.DIRECT_BOTTOM_TOP);
        TextView tv_sale = (TextView) view.findViewById(R.id.tv_sale);
        TextView tv_buy = (TextView) view.findViewById(R.id.tv_buy);
        TextView tv_cancel = (TextView) view.findViewById(R.id.tv_cancel);
        tv_sale.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (popupWinow != null) {
                    popupWinow.dismiss();
                }
                Intent intent = new Intent(MessageDetailActivity.this, ProductListActivity.class);
                intent.putExtra("buyer", userId);
                intent.putExtra("supplier", mineId);
                startActivity(intent);
            }
        });
        tv_buy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (popupWinow != null) {
                    popupWinow.dismiss();
                }
                Intent intent = new Intent(MessageDetailActivity.this, ProductListActivity.class);
                intent.putExtra("buyer", mineId);
                intent.putExtra("supplier", userId);
                startActivity(intent);
            }
        });
        tv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (popupWinow != null) {
                    popupWinow.dismiss();
                }
            }
        });

        popupWinow.showAtLocation(findViewById(R.id.root_view), Gravity.BOTTOM, 0, 0);
    }
}
